package com.cloudwise.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudwise.dao.MenuDao;
import com.cloudwise.entity.Menu;
import com.cloudwise.service.MenuService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;

/**
 * (Menu)表服务实现类
 *
 * @author makejava
 * @since 2021-12-03 09:16:48
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {
    @Resource
    private MenuDao menuDao;
    @Override
    public List<Tree<String>> getTreeMenu() throws IllegalAccessException {
        //声明一个menu类型的树节点集
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //获取数据库所有menu
        List<Menu> allMenu = menuDao.getAllMenu();
        //将menu对象放入树节点
        for (Menu menu : allMenu) {
            Menu parentMenu = this.getById(menu.getParentId());

            Map<String,Object> map = new HashMap<>();
            Field[] fields = menu.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                map.put(field.getName(),field.get(menu));
            }
            if(ObjectUtil.isNotNull(parentMenu)){
                map.put("parentName",parentMenu.getMenuName());
            }else {
                map.put("parentName","请选择上级目录");
            }
            TreeNode<String> treeNode = new TreeNode<>();
            treeNode.setId(String.valueOf(menu.getMenuId()));
            treeNode.setName(menu.getMenuName());
            treeNode.setParentId(String.valueOf(menu.getParentId()));
            treeNode.setWeight(menu.getSort());
            treeNode.setExtra(map);
            nodeList.add(treeNode);
        }
        //树节点配置
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("menuId");
        config.setWeightKey("sort");
        config.setParentIdKey("parentId");
        config.setNameKey("menuName");
//        config.setDeep(4);
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", config,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    tree.putExtra("url", treeNode.getExtra().get("url"));
                    tree.putExtra("menuType",treeNode.getExtra().get("menuType"));
                    tree.putExtra("remark",treeNode.getExtra().get("remark"));
                    tree.putExtra("permission",treeNode.getExtra().get("permission"));
                    tree.putExtra("creator",treeNode.getExtra().get("creator"));
                    tree.putExtra("createTime",treeNode.getExtra().get("createTime"));
                    tree.putExtra("updater",treeNode.getExtra().get("updater"));
                    tree.putExtra("updateTime",treeNode.getExtra().get("updateTime"));
                    tree.putExtra("icon",treeNode.getExtra().get("icon"));
                    tree.putExtra("visible",treeNode.getExtra().get("visible"));
                    tree.putExtra("parentName",treeNode.getExtra().get("parentName"));
                });
        return treeNodes;
    }

    @Override
    public List<Tree<String>> getLayuiTree() {
        //获取数据库所有menu
        List<Menu> allMenu = menuDao.getAllMenu();
        List<Tree<String>> treeNodes = getTreeByMenus(allMenu);
        return treeNodes;
    }

    @Override
    public List<Tree<String>> getAllMenuByLoginName(String loginName) {
        List<Menu> allMenuByLoginName = menuDao.getAllMenuByLoginName(loginName);
        List<Tree<String>> treeNodes = getTreeByMenus(allMenuByLoginName);
        return treeNodes;
    }


    @Override
    public List<Tree<String>> getTreeByMenus(List<Menu> menus) {
        //声明一个menu类型的树节点集
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //将menu对象放入树节点
        for (Menu menu : menus) {
            TreeNode<String> treeNode = new TreeNode<>();
            Map<String,Object> map = new HashMap<>();
            map.put("url",menu.getUrl());
            map.put("icon",menu.getIcon());
            treeNode.setId(String.valueOf(menu.getMenuId()));
            treeNode.setName(menu.getMenuName());
            treeNode.setParentId(String.valueOf(menu.getParentId()));
            treeNode.setWeight(menu.getSort());
            treeNode.setExtra(map);
            nodeList.add(treeNode);
        }
        //树节点配置
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("id");
        config.setNameKey("title");
        config.setWeightKey("sort");
        config.setParentIdKey("parentId");
//        config.setDeep(4);
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", config,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setName(treeNode.getName());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.putExtra("url",treeNode.getExtra().get("url"));
                    tree.putExtra("icon",treeNode.getExtra().get("icon"));
                });
        return treeNodes;
    }

    @Override
    public Set<String> getAllPermsByLoginName(String loginName) {
        List<Menu> menuList = menuDao.getAllMenuByLoginName(loginName);
        Set<String> perms=new HashSet<>();
        for (Menu menu : menuList) {
            String menuPerms = menu.getPermission();
            if(null!=menuPerms && menuPerms.length()>0){
                perms.add(menuPerms);
            }
        }
        return perms;
    }

    @Override
    public Set<String> getAllUrl() {
        List<Menu> allMenu = menuDao.getAllMenu();
        Set<String> urls = new HashSet<>();
        for (Menu menu : allMenu) {
            String url = menu.getUrl();
            if(null!=url && url.length()>0 && !url.equals("#")){
                urls.add(url);
            }
        }
        return urls;
    }

    @Override
    public String getPermByUrl(String url) {
        List<Menu> allMenu = menuDao.getAllMenu();
        for (Menu menu : allMenu) {
            if(menu.getUrl().equals(url)){
                return "perms["+menu.getPermission()+"]";
            }
        }
        return "";
    }

    @Override
    public List<Tree<String>> getAllTreeMenuIds(Integer roleId) {
        List<Menu> menus = menuDao.getAllMenuByRoleId(roleId);
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //将menu对象放入树节点
        for (Menu menu : menus) {
            TreeNode<String> treeNode = new TreeNode<>();
            treeNode.setId(String.valueOf(menu.getMenuId()));
            treeNode.setName(menu.getMenuName());
            treeNode.setParentId(String.valueOf(menu.getParentId()));
            treeNode.setWeight(menu.getSort());
            nodeList.add(treeNode);
        }
        //树节点配置
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("id");
        config.setNameKey("title");
        config.setWeightKey("sort");
        config.setParentIdKey("parentId");
//        config.setDeep(4);
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", config,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setName(treeNode.getName());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                });
        return treeNodes;
    }
}

